package com.xl333.mysql.dao;

import com.xl333.mysql.model.Table;

import java.util.List;

/**
 * @author 七月的狮子
 * @date 2020/5/13
 */
public class TableDao {

    private static TableDao instance = null;

    private TableDao(){

    }

    public static TableDao getInstance(){
        if(instance == null){
            instance = new TableDao();
        }
        return instance;
    }

    /**
     * 根据schema与表名查询表信息
     * @param schema
     * @param tableNames 字符串数组
     * @return
     */
    public List<Table> queryBySchemaAndName(String schema, String [] tableNames){
        String sql = "select TABLE_SCHEMA 'schema', TABLE_NAME 'name', TABLE_COMMENT 'comment'" +
                " from TABLES where TABLE_SCHEMA = '" + schema + "'";
        if (tableNames != null && tableNames.length > 0) {
            StringBuilder builder = new StringBuilder();
            for (String tableName : tableNames) {
                builder.append("'" + tableName + "',");
            }
            sql +=  " and TABLE_NAME in (" + builder.substring(0, builder.length() - 1) + ")";
        }
        DaoManager<Table> daoManager = new DaoManager<>();
        return daoManager.query(sql, Table.class);
    }

}
